package cn.zhz.springdoc.springboot.service.impl;

import cn.hutool.json.JSONUtil;
import cn.zhz.springdoc.core.dto.ApiPushDto;
import cn.zhz.springdoc.core.enums.OpenApiParserEnum;
import cn.zhz.springdoc.springboot.config.v3.CustomerMultipleOpenApiWebMvcResource;
import cn.zhz.springdoc.springboot.property.OpenApiProperty;
import cn.zhz.springdoc.springboot.service.IOpenApiParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ZHZ
 * @since 2022-11-18
 */
@Slf4j
@RequiredArgsConstructor
public class OpenApi3ParserImpl implements IOpenApiParser {

    private final CustomerMultipleOpenApiWebMvcResource customerMultipleOpenApiWebMvcResource;

    private final OpenApiProperty openApiProperty;

    /**
     * 解析器名称
     *
     * @return
     */
    @Override
    public OpenApiParserEnum name() {
        return OpenApiParserEnum.API_3;
    }

    /**
     * 解析
     *
     * @return
     */
    @Override
    public ApiPushDto parser() {
        String openApiJson = "";
        String groupName = openApiProperty.getGroupName();
        try {
            openApiJson = customerMultipleOpenApiWebMvcResource.openapiJson(groupName);
        } catch (JsonProcessingException e) {
            log.error("获取OpenApi错误", e);
            return null;
        }

        return new ApiPushDto(this.name().getCode(),openApiProperty.getProjectName(), openApiProperty.getGroupName(), JSONUtil.toJsonStr(openApiJson));

    }
}
